package com.jilian.mydemo.modul.order.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.contrarywind.view.WheelView;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.MemberDto;
import com.jilian.mydemo.common.dto.OrderDetailDto;
import com.jilian.mydemo.dialog.nicedialog.BaseNiceDialog;
import com.jilian.mydemo.dialog.nicedialog.NiceDialog;
import com.jilian.mydemo.dialog.nicedialog.ViewConvertListener;
import com.jilian.mydemo.dialog.nicedialog.ViewHolder;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.order.adapter.HydraulicListAdapter;
import com.jilian.mydemo.modul.order.adapter.WheelAdapter;
import com.jilian.mydemo.modul.order.adapter.WorkWheelAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;

import java.util.ArrayList;
import java.util.List;

/**
 * 水工列表
 */
public class HydraulicListActivity extends BaseActivity implements CustomItemClickListener {
    private RecyclerView recyclerview;
    private HydraulicListAdapter adapter;
    private List<MemberDto> datas;
    private List<String> selectDatas;
    private LinearLayoutManager linearLayoutManager;
    private OrderViewModel viewModel;
    private OrderDetailDto orderDetailDto;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_hydrauliclist;
    }

    @Override
    public void initView() {
        setNormalTitle("水工列表", v -> finish());
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        adapter = new HydraulicListAdapter(this, datas, this);
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void initData() {
        orderDetailDto = (OrderDetailDto) getIntent().getSerializableExtra("orderDetailDto");
        showLoadingDialog();
        queryMember(getIntent().getStringExtra("id"));

    }

    /**
     * 获取水工列表
     */
    private void queryMember(String id) {
        viewModel.queryMember(id);
        viewModel.getMemberliveData().observe(this, new Observer<BaseDto<List<MemberDto>>>() {
            @Override
            public void onChanged(@Nullable BaseDto<List<MemberDto>> listBaseDto) {
                hideLoadingDialog();
                if (listBaseDto.isSuccess()) {
                    if (EmptyUtils.isNotEmpty(listBaseDto.getData())) {
                        datas.clear();
                        datas.addAll(listBaseDto.getData());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUitl.showImageToastTips(listBaseDto.getMsg());
                }
            }
        });
    }

    @Override
    public void initListener() {

    }

    @Override
    public void onItemClick(View view, int position) {
        if (orderDetailDto == null) {
            ToastUitl.showImageToastTips("订单详情不能为空");
            return;
        }
        if (!("1".equals(orderDetailDto.getIsLeader()))) {
            ToastUitl.showImageToastTips("只有队长可以操作");
            return;
        }
        showRefundReasonDialog(position);
    }

    /**
     * 退单对话框
     */
    private void showRefundReasonDialog(int position) {

        NiceDialog.init()
                .setLayoutId(R.layout.dialog_select_courier)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        dialog.setOutCancel(false);
                        WheelView wheelview = (WheelView) holder.getView(R.id.wheelview);
                        wheelview.setCyclic(false);
                        TextView tvCancel = (TextView) holder.getView(R.id.tv_cancel);
                        TextView tvFinish = (TextView) holder.getView(R.id.tv_finish);


                        TextView tvTitle = (TextView) holder.getView(R.id.tv_title);
                        if ("1".equals(datas.get(position).getBillingType())) {
                            tvTitle.setText("选择工时");
                            selectDatas = new ArrayList<>();
                            selectDatas.add("1小时");
                            selectDatas.add("2小时");
                            selectDatas.add("3小时");
                            selectDatas.add("4小时");
                            selectDatas.add("5小时");
                        }
                        if ("2".equals(datas.get(position).getBillingType())) {
                            tvTitle.setText("选择完成件数");
                            selectDatas = new ArrayList<>();
                            selectDatas.add("1件");
                            selectDatas.add("2件");
                            selectDatas.add("3件");
                            selectDatas.add("4件");
                            selectDatas.add("5件");
                            selectDatas.add("6件");
                            selectDatas.add("7件");
                            selectDatas.add("8件");
                            selectDatas.add("9件");
                            selectDatas.add("10件");
                            selectDatas.add("11件");
                            selectDatas.add("12件");
                            selectDatas.add("13件");
                            selectDatas.add("14件");
                            selectDatas.add("15件");
                            selectDatas.add("16件");
                            selectDatas.add("17件");
                            selectDatas.add("18件");
                            selectDatas.add("19件");
                            selectDatas.add("20件");
                        }

                        wheelview.setAdapter(new WheelAdapter(selectDatas));

                        tvFinish.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                updateWork(position, String.valueOf(wheelview.getCurrentItem() + 1), wheelview);
                                dialog.dismiss();
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                            }
                        });
                        wheelview.setCurrentItem(0);
                    }
                })
                .setShowBottom(true)
                .show(getSupportFragmentManager());
    }

    /**
     * 设置工作量
     *
     * @param position
     * @param workload
     * @param wheelview
     */
    private void updateWork(int position, String workload, WheelView wheelview) {
        showLoadingDialog();
        viewModel.updateWork(datas.get(position).getId(), workload);
        viewModel.getUpdateWorkliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto baseDto) {
                if (baseDto.isSuccess()) {
                    if ("1".equals(datas.get(position).getBillingType())) {
                        datas.get(position).setWorkload(String.valueOf(wheelview.getCurrentItem() + 1) + "小时");

                    }
                    if ("2".equals(datas.get(position).getBillingType())) {
                        datas.get(position).setWorkload(String.valueOf(wheelview.getCurrentItem() + 1) + "件");
                    }


                    adapter.notifyDataSetChanged();
                }
                hideLoadingDialog();
                ToastUitl.showImageToastTips(baseDto.getMsg());
            }
        });
    }
}
